package org.example.demo1.utils;

import org.apache.ibatis.session.SqlSession;

import org.example.demo1.DAO.UserDao;
import org.example.demo1.Entity.User;

public class InquireTools {



    public static User getUserInformation(String username) {
        User user = null;
        SqlSession sqlSession = null;
        try {

            sqlSession = MybatisUtils.getSqlSession();
            //方式一：getMapper
            UserDao mapper = sqlSession.getMapper(UserDao.class);
            user = mapper.getUserInformation(username);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {

            //关闭sqlSession
            sqlSession.close();
            return user;
        }
    }

    public static void InsertUser(String username, String password, String email, String phone) {
        SqlSession sqlSession = null;

        try {

            sqlSession = MybatisUtils.getSqlSession();
            //方式一：getMapper
            UserDao mapper = sqlSession.getMapper(UserDao.class);
            mapper.insertUser(username, password, email, phone);
            //手动提交事务
            sqlSession.commit();

        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            //关闭sqlSession
            sqlSession.close();

        }
    }

    public static User getUserInformationByPhone(String phone) {
        User user = null;
        SqlSession sqlSession = null;
        try {

            sqlSession = MybatisUtils.getSqlSession();
            //方式一：getMapper
            UserDao mapper = sqlSession.getMapper(UserDao.class);
            user = mapper.getUserInformationByPhone(phone);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {

            //关闭sqlSession
            sqlSession.close();
            return user;
        }
    }
    public static User getUserInformationByEmail(String email) {
        User user = null;
        SqlSession sqlSession = null;
        try {

            sqlSession = MybatisUtils.getSqlSession();
            //方式一：getMapper
            UserDao mapper = sqlSession.getMapper(UserDao.class);
            user = mapper.getUserInformationByEmail(email);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {

            //关闭sqlSession
            sqlSession.close();
            return user;
        }
    }

    public static User getUserInformationByPhoneAndUsername(String username,String phone) {
        User user = null;
        SqlSession sqlSession = null;
        try {

            sqlSession = MybatisUtils.getSqlSession();
            //方式一：getMapper
            UserDao mapper = sqlSession.getMapper(UserDao.class);
            user = mapper.getUserInformationByPhoneAndUsername(username,phone);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {

            //关闭sqlSession
            sqlSession.close();
            return user;
        }
    }

    public static User getUserInformationByEmailAndUsername(String usrename,String email) {
        User user = null;
        SqlSession sqlSession = null;
        try {

            sqlSession = MybatisUtils.getSqlSession();
            //方式一：getMapper
            UserDao mapper = sqlSession.getMapper(UserDao.class);
            user = mapper.getUserInformationByEmailAndUsername(usrename,email);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {

            //关闭sqlSession
            sqlSession.close();
            return user;
        }
    }



    public static boolean updateUserInformation(String username,String password) {
        SqlSession sqlSession = null;
        User u=null;
        sqlSession = MybatisUtils.getSqlSession();
        //方式一：getMapper
        UserDao mapper = sqlSession.getMapper(UserDao.class);
        try {
            u=mapper.getUserInformation(username);
            if (u.getPassword()==password) {
                return true;
            }
            mapper.updateUserInformation(username,password);
            //手动提交事务
            sqlSession.commit();
        }catch(Exception e) {
            e.printStackTrace();
        } finally {
            String Vpassword=mapper.getUserInformation(username).getPassword();
            //关闭sqlSession
            sqlSession.close();
            if (Vpassword.equals(password)) return true;   else  return false;
        }
    }

   

}
